home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 283_01 / demo.c < prev    next >
C/C++ Source or Header  |  1988-09-22  |  9KB  |  264 lines

  1. /* demo.c -- demonstrate some of the Fafnir library's forms editing routines 
  2.    September 10, 1988
  3.    David C. Oshel
  4.    */
  5.  
  6.  
  7. #include "ciao.h"        /* screen & keyboard i/o, supporting fns & macros */
  8. #include "fafnir.h"      /* magic forms macros, etc. */
  9. #include <stdarg.h>
  10.  
  11.  
  12. #define PRETEND UPDATE   /* pretend we're going to update records */
  13.  
  14.                    /*  ....v....1....v  */
  15. static char title[] = "Fafnir ver. 2.0 - elysian fields and forms nirvana by d.c.oshel";
  16.  
  17. /* forward references to functions named in DEFINE_FIELDS()
  18.    */
  19. static int difference( char *p, int x, int y, int len );
  20. static int concatname( char *p, int x, int y, int len );
  21.  
  22.  
  23.  
  24. /* Field Type macros (ain't C sweet?)
  25.    */
  26.  
  27. FIELDS last, first, conam, addr1, addr2, city, state, zip, homef, workf,
  28.        today, name, dues, paid, blnc, saltn, rem;
  29.     
  30. DEFINE_FIELDS( fafnir )
  31.  
  32.     /* Note: do not put trailing commas after closing parenthesis! */
  33.  
  34.     /* Page 1, starts at offset 0, has 11 fields */
  35.  
  36. #define PAGE1 TOP_OF_FORM
  37.  
  38.     TODAY(      today,                              17, 15     )   /* 0 */
  39.     TEXT_SET(   last,   "Oshel",                    17,  3, 30 )   /* 1 */
  40.     TEXT_SET(   first,  "David C.",                 17,  4, 30 )   /* 2 */
  41.     TEXT_SET(   conam,  "MicroConsulting Services", 17,  5, 30 )   /* 3 */
  42.     TEXT_SET(   addr1,  "1219 Harding Avenue",      17,  6, 30 )   /* 4 */
  43.     TEXT(       addr2,                              17,  7, 30 )   /* 5 */
  44.     TEXT_SET(   city,   "Ames",                     17,  8, 30 )   /* 6 */ 
  45.     STATE_SET(  state,  "IA",                       17,  9     )   /* 7 */
  46.     TENZIP_SET( zip,    "50010",                    17, 10     )   /* 8 */
  47.  
  48.     /* note that cursor follows field element order, here! */
  49.  
  50.     PHONE_SET( workf, "(515) 292-    ", 17, 12 )       /* first  9 */
  51.     PHONE_SET( homef, "(515) 233-    ", 17, 11 )       /* then  10 */
  52.  
  53. #define RANGE1 11  /* be very, very careful about this number! */
  54.  
  55.     /* Page 2, starts at offset 11, has 6 fields */
  56.  
  57. #define PAGE2 RANGE1
  58.  
  59.     VIRTUAL(      name,           17,  4, 30, concatname      ) /* 11, 0 */
  60.     DOLLAR_SET(   dues,  "10.00", 17,  6                      ) /* 12, 1 */
  61.     DOLLAR(       paid,           17,  7                      ) /* 13, 2 */
  62.     COMPUTE_SET(  blnc,  "10.00", 17,  8, DL_SIZE, difference ) /* 14, 3 */
  63.     TEXT_SET(     saltn, "Dave",  17, 15, 20                  ) /* 15, 4 */
  64.     TEXT_SET(     rem,   title,   17, 17, SLIDE(15)           ) /* 16, 5 */
  65.  
  66. #define RANGE2 6  /* don't guess, count, count, count, and then RECOUNT! */
  67.  
  68. END_FIELDS;
  69.  
  70.  
  71. /*-------------------------------------------------------------------
  72.   Number of fields: this argument will be passed to AllocateFields()
  73.  
  74.                           *** DANGER *** 
  75.                           CRITICAL VALUE!
  76.  
  77.    Alternate method for computing number of fields to allocate: 
  78.  
  79.                    #define ALL FormSize(fafnir)
  80.  
  81.    This works, so long as you're not doing anything fancy with 
  82.    duplicate field names.  Be advised that allocating too many
  83.    or too few fields is guaranteed to crash the system!
  84. ---------------------------------------------------------------------*/
  85.  
  86. #define ALL RANGE1+RANGE2   /* <-- don't add this in your head, stupid! */
  87.  
  88.  
  89.  
  90.  
  91. /*=================== Utilities ==================*/
  92.  
  93. /* global system error handler */
  94.  
  95. void bomb( char *msg, ... )
  96. {
  97.     va_list arg_ptr;
  98.  
  99.     printf("\a\n*** Error: ");
  100.     va_start( arg_ptr, msg );
  101.     vprintf( msg, arg_ptr );
  102.     va_end( arg_ptr );
  103.     exit (1);
  104. }
  105.  
  106.  
  107.  
  108. /* true computed field, difference of two other fields */
  109. static int difference( char *p, int x, int y, int len )
  110. {
  111.     long n1, n2, bal;
  112.     char *q, *r, temp[48];
  113.  
  114.     n1 = atoln( dues, len );
  115.     n2 = -1L * atoln( paid, len );
  116.  
  117.     bal = n1 + n2;
  118.  
  119.     q = dlrcvt( bal );        /* make it canonical 0.00 format */ 
  120.     r = strchr( q, '\0' );    /* remove trailing minus or blank */
  121.     r--;
  122.     *r = '\0';
  123.  
  124.     if ( bal < 0L )
  125.     {
  126.         r = malloc(len);
  127.         strcpy( r, "-" );
  128.         strcat( r,q );
  129.         sprintf( temp, "%*s", len, r );  /* right justify, ---> */
  130.         free(r);
  131.     }
  132.     else
  133.         sprintf( temp, "%*s", len, q );   /* right justify, ---> */
  134.  
  135.     if ( strlen(temp) != len )  /* do not alter field */
  136.         strset( p, '$' );       /* field overflow */
  137.     else
  138.         strcpy( p,temp );
  139.  
  140.     return (1);
  141. }
  142.  
  143.  
  144. /* "validate" virtual field by concatenating two other fields */
  145.  
  146. static int concatname( char *p, int x, int y, int len )
  147. {
  148.     register int i,j;
  149.  
  150.     memcpy( p, first, len );
  151.  
  152.     for ( i = len - 1; i > 0; i-- )
  153.     {
  154.         if ( *(p + i) != ' ' )
  155.            break;
  156.     }
  157.  
  158.     if ( *p != ' ' ) 
  159.     {
  160.         i++;
  161.         i++;
  162.     }
  163.  
  164.     for ( j = 0; j < 30 && i < 30; i++, j++ )
  165.     {
  166.         *( p + i ) = *( last + j );
  167.     }
  168.  
  169.     return (1);
  170. }
  171.  
  172.  
  173.  
  174. main()
  175. {
  176.     extern char DamageBuffer[];
  177.     extern char DamageControlMsg[];
  178.  
  179.     static char *dialogue[] = {
  180.         "Continue demonstration",
  181.         "Exit program"
  182.     };
  183.  
  184.     vid_init(0);  /* *** MUST *** */
  185.  
  186.     AllocateFields( fafnir, ALL );  /* *** MUST *** */
  187.  
  188.     /* would normally move the data record into form fields at this
  189.        point, but we're faking the UPDATE with an initialized form
  190.        */
  191.  
  192.     do
  193.     {
  194.         zoo:
  195.         switch ( TwoPageForm( GetPage( "FAF1.S", fafnir, PAGE1, RANGE1 ), 
  196.                               GetPage( "FAF2.S", fafnir, PAGE2, RANGE2 ), 
  197.                               PRETEND ) )
  198.         {
  199.         case SAVE_FORM:
  200.             /* would normally move the form fields back into the data
  201.                record at this point, but we're just pretending
  202.                */
  203.             boxmsg( "Pretending to save this form!" );
  204.             break;
  205.         case DELETE_FORM:  /* only if OLDUP */
  206.             boxmsg( "You can't delete a demonstration form!");
  207.         case SKIP_FORM:
  208.             nboxmsg( "Searching..." );  /* fake search for OLDUP */
  209.             goto zoo;
  210.             break;
  211.         case STOP_SEARCH:  /* only if OLDUP */
  212.             boxmsg( "What, just like that?!" );
  213.             break;
  214.         }
  215.     }
  216.     while ( (blopbloop(),select( 25,1,dialogue,2 )) == 0 );
  217.  
  218.     vid_exit();  /* restore sign-on screen */
  219.  
  220. #if defined (TEST)
  221.  
  222.     /* check to make sure the formedit module has not fouled up somehow
  223.        */
  224.     printf( "If the program screwed up and got outside a field dimension,\n");
  225.     printf( "there will be an F beside a wrong number beside the field name.\n");
  226.  
  227.     printf( "last   %2d  %c\n", strlen(last ), ((strlen(last ) == 30)? 'T':'F'));
  228.     printf( "first  %2d  %c\n", strlen(first), ((strlen(first) == 30)? 'T':'F'));
  229.     printf( "conam  %2d  %c\n", strlen(conam), ((strlen(conam) == 30)? 'T':'F'));
  230.     printf( "addr1  %2d  %c\n", strlen(addr1), ((strlen(addr1) == 30)? 'T':'F'));
  231.     printf( "addr2  %2d  %c\n", strlen(addr2), ((strlen(addr2) == 30)? 'T':'F'));
  232.     printf( "city   %2d  %c\n", strlen(city ), ((strlen(city ) == 30)? 'T':'F'));
  233.     printf( "state  %2d  %c\n", strlen(state), ((strlen(state) ==  2)? 'T':'F'));
  234.     printf( "zip    %2d  %c\n", strlen(zip  ), ((strlen(zip  ) == 10)? 'T':'F'));
  235.     printf( "homef  %2d  %c\n", strlen(homef), ((strlen(homef) == 14)? 'T':'F'));
  236.     printf( "workf  %2d  %c\n", strlen(workf), ((strlen(workf) == 14)? 'T':'F'));
  237.     printf( "today  %2d  %c\n", strlen(today), ((strlen(today) ==  8)? 'T':'F'));
  238.     printf( "name   %2d  %c\n", strlen(name ), ((strlen(name ) == 30)? 'T':'F'));
  239.     printf( "dues   %2d  %c\n", strlen(dues ), ((strlen(dues ) == 15)? 'T':'F'));
  240.     printf( "paid   %2d  %c\n", strlen(paid ), ((strlen(paid ) == 15)? 'T':'F'));
  241.     printf( "blnc   %2d  %c\n", strlen(blnc ), ((strlen(blnc ) == 15)? 'T':'F'));
  242.     printf( "saltn  %2d  %c\n", strlen(saltn), ((strlen(saltn) == 20)? 'T':'F'));
  243.     printf( "rem    %2d  %c\n", strlen(rem  ), ((strlen(rem  ) == 60)? 'T':'F'));
  244.  
  245.     printf("Total of %u fields on the form!\n\n",
  246.             (sizeof( fafnir )/sizeof(FORM_RULES)));
  247. #endif
  248.  
  249.     /*